﻿using FirebirdSql.Data.FirebirdClient;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using MySqlConnector;
using Npgsql;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Volo.Abp;
using Volo.Abp.DependencyInjection;

namespace YaAppoint.DappperConnection
{
    public class ConnectionProvider(IConfiguration configuration) : IConnectionProvider, ITransientDependency
    {
        private readonly IConfiguration _configuration = configuration;

        public IDbConnection GetDbConnection(string connectinString, DatabaseType databaseType)
        {
            return databaseType switch
            {
                DatabaseType.Oracle => new OracleConnection(connectinString),
                DatabaseType.MySql => new MySqlConnection(connectinString),
                DatabaseType.PostgreSql => new NpgsqlConnection(connectinString),
                DatabaseType.Firebird => new FbConnection(connectinString),
                DatabaseType.Db2 => throw new BusinessException(CustomErrorCodes.DB2NotSupported),
                _ => new SqlConnection(connectinString)
            };
        }

        // 获取数据库类型
        public DatabaseType GetDatabaseType()
        {
            var str = _configuration["ConnectionStrings:HisDbType"];
            return str switch
            {
                "SqlServer" => DatabaseType.SqlServer,
                "MySql" => DatabaseType.MySql,
                "Oracle" => DatabaseType.Oracle,
                "PostgreSql" => DatabaseType.PostgreSql,
                "Firebird" => DatabaseType.Firebird,
                "Db2" => DatabaseType.Db2,
                _=>DatabaseType.SqlServer
            };
        }
    }
}
